package org.easymodelers.gmf;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.easymodelers.gmf.creator.GmfGraphCreator;
import org.easymodelers.gmf.creator.GmfMapCreator;
import org.easymodelers.gmf.creator.GmfToolCreator;
import org.easymodelers.modeler.data.ModelerDefinition;
import org.easymodelers.processing.ModelerUpdate;
import org.easymodelers.processing.PreModelsCreator;

public class GmfPreModelsCreator implements PreModelsCreator {
	private GmfGraphCreator graphCreator;
	private GmfToolCreator toolCreator;
	private GmfMapCreator mapCreator;

	public List<ModelerUpdate> createPreModels(String modelPath, ModelerDefinition modeler) throws IOException {
		if (!modelPath.endsWith(File.separator)) {
			modelPath += File.separator;
		}

		String modelerName = modeler.getName();
		if (modelerName==null || "".equals(modelerName.trim())) {
			String message = "The name of the modeler must be specified.";
			throw new GmfModelGenerationException(message);
		}
		
		String graphModelPath = modelPath + modeler.getName() + ".gmfgraph";
		String toolModelPath = modelPath + modeler.getName() + ".gmftool";
		String mapModelPath = modelPath + modeler.getName() + ".gmfmap";
		
		List<ModelerUpdate> updates = new ArrayList<ModelerUpdate>();
		List<ModelerUpdate> graphUpdates = graphCreator.createGmfGraph(graphModelPath, modeler);
		updates.addAll(graphUpdates);
		List<ModelerUpdate> toolUpdates = toolCreator.createGmfTool(toolModelPath, modeler);
		updates.addAll(toolUpdates);
		List<ModelerUpdate> mapUpdates = mapCreator.createGmfMap(mapModelPath, modeler);
		updates.addAll(mapUpdates);
		return updates;
	}

	public GmfGraphCreator getGraphCreator() {
		return graphCreator;
	}

	public void setGraphCreator(GmfGraphCreator graphCreator) {
		this.graphCreator = graphCreator;
	}

	public GmfToolCreator getToolCreator() {
		return toolCreator;
	}

	public void setToolCreator(GmfToolCreator toolCreator) {
		this.toolCreator = toolCreator;
	}

	public GmfMapCreator getMapCreator() {
		return mapCreator;
	}

	public void setMapCreator(GmfMapCreator mapCreator) {
		this.mapCreator = mapCreator;
	}

}
